#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include "Log.hpp"

namespace dict
{
    const std::string defaultPath = "./dict.txt";
    class Dict
    {
    public:
        Dict(std::string path = defaultPath)
            :_path(path)
        {}
        ~Dict()
        {}

        void LoadDict()
        {
            std::ifstream in(_path, std::ios_base::in);
            if(!in.is_open())
                LOG(FATAL, "read dict file fail\n");
            
            std::string line;
            while(std::getline(in, line))
            {
                if(line.empty()) continue;
                size_t pos = line.find(':');
                if(pos == std::string::npos) continue;
                _dict.insert({line.substr(0, pos), line.substr(pos+1)});
            }
            in.close();
        }

        void DebugPrint()
        {
            for(auto it : _dict)
            {
                std::cout << it.first << ":" << it.second << std::endl;
            }
        }

        std::string Translate(std::string word)
        {
            std::unordered_map<std::string, std::string>::iterator it = _dict.find(word);
            if(it == _dict.end()) return "NOT FOUND";
            return _dict[word];
        }

    private:
        std::unordered_map<std::string, std::string> _dict;
        std::string _path;
    };
}